package com.kevin.utility.common;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class MapUtils {
	public static void setValue(Map map, Object thisObj) {
		Set set = map.keySet();
		Iterator iterator = set.iterator();
		while (iterator.hasNext()) {
			Object obj = iterator.next();
			Object val = map.get(obj);
			setMethod(obj, val, thisObj);
		}
	}

	public static void setMethod(Object method, Object value, Object thisObj) {
		Class c;
		try {
			c = Class.forName(thisObj.getClass().getName());
			String met = (String) method;
			met = met.trim();
			if (!met.substring(0, 1).equals(met.substring(0, 1).toUpperCase())) {
				met = met.substring(0, 1).toUpperCase() + met.substring(1);
			}
			if (!String.valueOf(method).startsWith("set")) {
				met = "set" + met;
			}
			Class types[] = new Class[1];
			types[0] = Class.forName("java.lang.String");
			Method m = c.getMethod(met, types);
			m.invoke(thisObj, value);
		} catch (Exception e) {
			 
		}
	}

	public static Map getValue(Object thisObj) {
		Map map = new HashMap();
		Class c;
		try {
			c = Class.forName(thisObj.getClass().getName());
			Method[] m = c.getMethods();
			for (int i = 0; i < m.length; i++) {
				String method = m[i].getName();
				if (method.startsWith("get")) {
					try {
						Object value = m[i].invoke(thisObj);
						if (value != null) {
							String key = method.substring(3);
							key = key.substring(0, 1).toUpperCase() + key.substring(1);
							map.put(method, value);
						}
					} catch (Exception e) {
						// TODO: handle exception
						System.out.println("error:" + method);
					}
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return map;
	}
	
	public static Object Map2Object(Map map, Object o){
		
		
		return o;
	}
	
	
	 public static Map beanToMap(Object bean) 
	            throws IntrospectionException, IllegalAccessException, InvocationTargetException { 
	        Class type = bean.getClass(); 
	        Map returnMap = new HashMap(); 
	        BeanInfo beanInfo = Introspector.getBeanInfo(type); 

	        PropertyDescriptor[] propertyDescriptors =  beanInfo.getPropertyDescriptors(); 
	        for (int i = 0; i< propertyDescriptors.length; i++) { 
	            PropertyDescriptor descriptor = propertyDescriptors[i]; 
	            String propertyName = descriptor.getName(); 
	            if (!propertyName.equals("class")) { 
	                Method readMethod = descriptor.getReadMethod(); 
	                if(readMethod == null ){
	                	continue;
	                }
	                Object result = readMethod.invoke(bean, new Object[0]); 
	                if (result != null) { 
	                    returnMap.put(propertyName, result); 
	                } else { 
	                    returnMap.put(propertyName, ""); 
	                } 
	            } 
	        } 
	        return returnMap; 
	    }		
	 
	 public static void mapToBean( Map map, Object bean)  { 
		 try {
			 Class type = bean.getClass(); 
			 BeanInfo beanInfo;
	
			 beanInfo = Introspector.getBeanInfo(type);
		
			 PropertyDescriptor[] propertyDescriptors =  beanInfo.getPropertyDescriptors(); 
			 for (int i = 0; i< propertyDescriptors.length; i++) { 
				PropertyDescriptor descriptor = propertyDescriptors[i]; 
				String propertyName = descriptor.getName(); 
				if (!propertyName.equals("class")) { 
					Method writeMethod = descriptor.getWriteMethod(); 
					if(writeMethod == null ){
						continue;
					}
					Class<?>[] paramType = writeMethod.getParameterTypes();
					if(map.get(propertyName) == null || map.get(propertyName) == ""){
						continue;
					}
					Object property =  map.get(propertyName);
					if(property == null || "".equals(property)){
						continue;
					}
					if(paramType[0].getName().equals("java.util.Date")){
						writeMethod.invoke(bean, DateUtils.toDate(property.toString())); 
					}else if(paramType[0].getName().equals("java.lang.Integer")){
						writeMethod.invoke(bean, Integer.valueOf(property.toString())); 
					}else{
						writeMethod.invoke(bean, property); 
					}
					
					
				} 
			 }
		} catch (IntrospectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	} 
}
